/*
 * CNetworkSpeaker.h
 *
 *  Created on: 11.08.2011
 *      Author: kirill
 */

#ifndef CNETWORKSPEAKER_H_
#define CNETWORKSPEAKER_H_

#include "BasicTypes.h"
#include "IConnection.h"
#include "Constants.h"
#include "TThreadSafeQueue.h"
#include <string>
#include <boost/asio.hpp>
#include <boost/function.hpp>

/*
 *
 */
class CNetworkSpeaker
{
private:
	boost::asio::io_service io_service;
	boost::asio::ip::tcp::endpoint *endpoint;
	boost::asio::ip::tcp::acceptor *acceptor;
	boost::thread *IoservRun;

	TThreadSafeQueue<STANDART_NETWORK_MESSAGE*>* incomeQueue;

	bool           ClientFreeSlots[MAX_PLAYERS_COUNT]; // true = free, initial value

	boost::function <BYTE( IConnection*, const UINT )> connectionCreateCallback;
	IConnection* currentConnection;
	bool ConnectingStart;

	UINT FindWorkPort( const UINT FirstPort, const UINT LastPort );
	UINT GetFreeSlot();
public:
	CNetworkSpeaker();
	CNetworkSpeaker( TThreadSafeQueue<STANDART_NETWORK_MESSAGE*>* IncomeQueue );
	virtual ~CNetworkSpeaker();
	virtual bool ConnectionStatusCallback( const UINT ClientId, const UINT64 Error );
	virtual void StartConnection( const boost::asio::ip::address_v4 *IpAddress, const UINT FirstPort, const UINT LastPort,
			                      boost::function <BYTE( IConnection*, const UINT )> ConnectionCreateCallback );
	virtual bool GoOnConnecting();
	virtual void PauseConnecting();
};

#endif /* CNETWORKSPEAKER_H_ */
